﻿using MDT.LifeSewagePlatform.Mysql;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using MDT.LifeSewagePlatform.Conmmon;

namespace MDT.LifeSewagePlatform.Repository
{
    public class DrugDayRespository : BaseRepository<DrugDayEntity>, IDrugDayRespository
    {
        [Autowired]
        private SystemConfigEntity systemConfig { get; set; }
        public DrugDayRespository()
        {

        }
        public IQueryable<DrugDayView> GetDrugViews(Expression<Func<DrugDayView, bool>> expression)
        {
            var query = from drugday in _dbContext.DrugDays.AsNoTracking()
                        join area in _dbContext.Regions.AsNoTracking() on drugday.AreaID equals area.ID into Leftarea
                        from leftarea in Leftarea.DefaultIfEmpty()
                        join file in _dbContext.Files.AsNoTracking() on drugday.FileID equals file.ID
                        join city in _dbContext.Regions.AsNoTracking() on file.CityID equals city.ID
                        join company in _dbContext.Company.AsNoTracking() on file.CompanyID equals company.ID
                        join place in _dbContext.Details.AsNoTracking() on drugday.PlaceTypeID equals place.ID
                        select new DrugDayView()
                        {
                            AreaID = drugday.AreaID,
                            AreaName = leftarea==null ?"无":leftarea.DisplayName ,
                            Cocaine = Math.Round(drugday.Cocaine,2),
                            CompanyID = company.ID,
                            CompanyName = company.CompanyName,
                            Cyclohexanone =Math.Round(drugday.Cyclohexanone,2),
                            Ketamine = Math.Round(drugday.Ketamine,2),
                            MDMA = Math.Round(drugday.MDMA,2),
                            Morphine = Math.Round(drugday.Morphine,2),
                            MonitoringSite = drugday.MonitoringSite,
                            Quarter = drugday.Quarter,
                            Methylamphetamine =Math.Round(drugday.Methylamphetamine),
                            CityID = city.ID,
                            PlaceType = drugday.PlaceTypeID,
                            PlaceTypeName = place.RowName,
                            ID = drugday.ID,
                            MonitorTime = drugday.MonitorTime,
                            CityName = city.DisplayName,
                        };
            return query.Where(expression);
        }

        public IQueryable<DrugDayView> GroupBySite(IQueryable<DrugDayView> drugs)
        {
            return drugs.GroupBy(t => new { t.MonitoringSite, t.PlaceTypeName }).Select(t => new DrugDayView()
            {
                MonitoringSite = t.Key.MonitoringSite,
                Ketamine = t.Average(t => t.Ketamine),
                Cocaine = t.Average(t => t.Cocaine),
                Cyclohexanone = t.Average(t => t.Cocaine),
                Methylamphetamine = t.Average(t => t.Methylamphetamine),
                MDMA = t.Average(t => t.MDMA),
                PlaceTypeName = t.Key.PlaceTypeName,
                Morphine = t.Average(t => t.Morphine),
                ID = t.Max(t => t.ID),
            }).OrderBy(t => t.ID);
        }

        public async Task<List<DrugDayView>> GroupByDrugDays(IQueryable<DrugDayView> drugDays)
        {

            if (systemConfig.QuarterOrMonth == (int)eGroupGetDateType.Month)
            {
                return await drugDays.GroupBy(t => new { t.AreaID,t.MonitoringSite, t.AreaName, t.CityID, t.CompanyID, t.MonitorTime.Month }).
                               Select(t => new DrugDayView()
                               {
                                   AreaName = t.Key.AreaName,
                                   MonitoringSite = t.Key.MonitoringSite,
                                   Cyclohexanone = t.Average(t => t.Cyclohexanone),
                                   MDMA = t.Average(t => t.MDMA),
                                   Methylamphetamine = t.Average(t => t.Methylamphetamine),
                                   Ketamine = t.Average(t => t.Ketamine),
                                   Morphine = t.Average(t => t.Morphine),
                                   Cocaine = t.Average(t => t.Cocaine),
                                   AreaID = t.Key.AreaID,
                                   CompanyID = t.Key.CompanyID,
                                   CityID = t.Key.CityID,
                                   Quarter = t.Key.Month
                               }).
                               ToListAsync();
            }
            if (systemConfig.QuarterOrMonth == (int)eGroupGetDateType.Quarter)
            {
                return await drugDays.GroupBy(t => new { t.MonitoringSite, t.AreaName, t.CityID, t.CompanyID, t.AreaID, t.Quarter }).
                             Select(t => new DrugDayView()
                             {
                                 AreaName = t.Key.AreaName,
                                 MonitoringSite = t.Key.MonitoringSite,
                                 Cyclohexanone = t.Average(t => t.Cyclohexanone),
                                 MDMA = t.Average(t => t.MDMA),
                                 Methylamphetamine = t.Average(t => t.Methylamphetamine),
                                 Ketamine = t.Average(t => t.Ketamine),
                                 Morphine = t.Average(t => t.Morphine),
                                 Cocaine = t.Average(t => t.Cocaine),
                                 AreaID = t.Key.AreaID,
                                 CompanyID = t.Key.CompanyID,
                                 CityID = t.Key.CityID,
                                 Quarter = t.Key.Quarter
                             }).
                             ToListAsync();
            }
            return null;
        }
    }
}
